<?php

namespace app\modules\sign\controllers;

use yii;
use yii\web\Controller;
use app\modules\cruise\models\Member;
use yii\web\Session;

class MemberController extends Controller
{

    public function actionLogin()
    {   
        $s_member = \Yii::$app->session->get('member');
        if($s_member){
             return $this->redirect(['/sign/sign/index']);
        }
        return $this->render('login');
    }

    //检测手机号是否在数据库中
    public function actionCheck(){

    	$phone = Yii::$app->request->get("phone");
    	if($phone){
    		$member = Member::find()->where(['phone'=>$phone,'status'=>1])->one();
    		if($member){
    			echo 1;
    		}else{
    			echo 0;
    		}
    	}else{
    		echo 0;
    	}
    }


     // 获取手机验证码 
    public function actionCode($phone){
        if(empty($phone)){
            return 'phone is null';
        }
        $code  = rand(1000,9999);

        $session = Yii::$app->session;
        $session->set("php_code", $code); 

        // 发送短信
        $re = yii::$app->sms->sendSms($phone, $code);         

        $arr = json_decode(json_encode((array) simplexml_load_string($re)), true); 

        return $arr['returnstatus'];
    }


    //验证手机验证码
    public function actionVerify($phone,$code){

        if(empty($phone) || empty($code)){
        	$msg = ['code'=>0,'msg'=>'手机号或验证码为空'];
            print_r(json_encode($msg));
            return;
        }
        $session = Yii::$app->session;
        $sess_code = $session->get("php_code");

        if($sess_code == $code){
        	 $member = Member::find()->where(['phone'=>$phone,'status'=>1])->one();
        	 if($member){
        	 	$session->set('member', ['id'=>$member->id, 'phone'=>$member->phone, 'name'=>$member->name,'role'=>$member->role]);
        	 	$msg = ['code'=>1,'msg'=>'登录成功'];
	            print_r(json_encode($msg));
	            return;
        	 }else{
        	 	$msg = ['code'=>0,'msg'=>'手机号不存在'];
	            print_r(json_encode($msg));
	            return;
        	 }
        }else{
             $msg = ['code'=>0,'msg'=>'验证码错误'];
	         print_r(json_encode($msg));
	         return;
        }
        
    }

    //自动登录
    public function actionAutoLogin(){
         $data = \Yii::$app->request->post();
         $phone = isset($data['phone'])?$data['phone']:'';
         $session = \Yii::$app->session;
         $s_member = \Yii::$app->session->get('member');
         if($s_member){
            $msg = ['code'=>0,'msg'=>'已登录'];
            print_r(json_encode($msg));
            return ;
         }
         $member = Member::find()->where(['phone'=>$phone,'status'=>1])->one();
       
         if($member){
            $session->set('member', ['id'=>$member->id, 'phone'=>$member->phone, 'name'=>$member->name,'role'=>$member->role]);
            $msg = ['code'=>1,'msg'=>'登录成功'];
            print_r(json_encode($msg));
            return;
         }else{
            $msg = ['code'=>0,'msg'=>'手机号不存在'];
            print_r(json_encode($msg));
            return;
         }

    }


}
